package com.example.aop.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Slf4j
@Aspect
//@Component
public class AspectDemo {

    @Pointcut("execution(* com.example.aop.controller.*.*(..))")
    private void pt(){};

//    @Before("execution(* com.example.aop.controller.*.*(..))")
    @Before("pt()")
    public void doBefore() {
        log.info("AspectDemo -> doBefore...");
    }

//    @After("execution(* com.example.aop.controller.*.*(..))")
    @After("pt()")
    public void doAfter() {
        log.info("AspectDemo -> doAfter...");
    }

//    @AfterReturning("execution(* com.example.aop.controller.*.*(..))")
    @AfterReturning("pt()")
    public void doAfterReturning() {
        log.info("AspectDemo -> doAfterReturning...");
    }

//    @AfterThrowing("execution(* com.example.aop.controller.*.*(..))")
    @AfterThrowing("pt()")
    public void doAfterThrowing() {
        log.info("AspectDemo -> doAfterThrowing...");
    }

//    @Around("execution(* com.example.aop.controller.*.*(..))")
    @Around("pt()")
    public Object doAround(ProceedingJoinPoint joinPoint) {
        log.info("AspectDemo -> doAround before...");
        Object proceed = null;
        try {
            proceed = joinPoint.proceed();
            log.info("AspectDemo -> doAround after...");
        } catch (Throwable e) {
            log.error("发生异常, e: ", e);
        }
        return proceed;
    }
}
